﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Xml.Linq;
using Microsoft.Xna.Framework.Input;

namespace POB
{
    class World : GameScreen
    {
        public List<Unit> Units { get; set; }
        public List<IColideable> Colliders { get; set; }
        public Player LeftPlayer { get; set; }
        public ContentManager Content { get; set; }

        ResulutionManager resulutionManager;

        XElement leftXRace;

        public SpriteFont UI21 { get; set; }
        public SpriteFont UI36 { get; set; }
        public SpriteFont UI48 { get; set; }

        float t;

        Texture2D blankTex;

        public World(XElement leftXRace)
        {
            this.leftXRace = leftXRace;
        }

        public override void LoadContent()
        {
            Rectangle realScreen = new Rectangle(0, 0, ScreenManager.GraphicsDevice.Viewport.Width, ScreenManager.GraphicsDevice.Viewport.Height);
            resulutionManager = new ResulutionManager(ScreenManager.SpriteBatch, ScreenManager.GraphicsDevice, realScreen, new Rectangle(0, 0, 1920, 1080));

            this.Content = ScreenManager.Game.Content;
            this.Units = new List<Unit>();
            this.Colliders = new List<IColideable>();
            this.LeftPlayer = new Player(new Vector2(0,100),new Vector2(0,1), leftXRace, this);
            this.Content = ScreenManager.Game.Content;

            this.UI21 = Content.Load<SpriteFont>(@"Fonts\UI21");
            this.UI36 = Content.Load<SpriteFont>(@"Fonts\UI36");
            this.UI48 = Content.Load<SpriteFont>(@"Fonts\UI48");

            blankTex = Content.Load<Texture2D>(@"blank");

            base.LoadContent();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            Debugger.Clear();

            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            this.LeftPlayer.Update(elapsedTime);

            foreach(Unit unit in Units)
            {
                unit.Update(elapsedTime);
            }

            SimulatePhysics();

            Debugger.Write("t: ");
            Debugger.WriteLine(t.ToString());
            Debugger.Write("DebugTest");

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void HandleInput(InputState input)
        {
            this.LeftPlayer.HandleInput(input);
            t = input.CurrentGamePadStates[0].Triggers.Left;

            base.HandleInput(input);
        }

        public override void Draw(GameTime gameTime)
        {
            float elapsedTime = gameTime.ElapsedGameTime.Seconds;

            foreach(Unit unit in Units)
            {
                unit.Draw(elapsedTime, ScreenManager.SpriteBatch, resulutionManager.Matrix);
            }

            this.LeftPlayer.Draw(ScreenManager.SpriteBatch, resulutionManager.Matrix);


            DebugOutput();

            resulutionManager.Draw();


            base.Draw(gameTime);

        }

        private void DebugOutput()
        {
            #region Debugging
            ScreenManager.SpriteBatch.Begin(resulutionManager.Matrix);
            List<string> lines = Debugger.GetOutput();
            int lineCount = 0;

            //Hintergrund
            foreach (string line in lines)
            {
                ScreenManager.SpriteBatch.DrawString(UI36, line, new Vector2(0, 45 * lineCount), Color.Black);

                lineCount++;
            }
            ScreenManager.SpriteBatch.End();
            #endregion
        }

        #region Collision
        private void SimulatePhysics()
        {
            foreach(IColideable colider in Colliders)
            {
                foreach(IColideable checkColider in Colliders)
                {
                    if(colider == checkColider ||
                        colider.Component.Unit.IsLeftTeam == checkColider.Component.Unit.IsLeftTeam)
                    {
                        continue;
                    }

                    Colide(colider, checkColider);
                }
            }
        }

        private void Colide(IColideable colider, IColideable checkColider)
        {
            foreach(Circle cicle in colider.Circles)
            {
                foreach(Circle checkCircle in checkColider.Circles)
                {
                    if(Collision.CircleCircleIntersect(cicle.position, cicle.radius, checkCircle.position, checkCircle.radius))
                    {
                        Unit unit = colider.Component.Unit;
                        Unit checkUnit = checkColider.Component.Unit;

                        if(colider.AcceptTouchWith(checkUnit))
                            unit.Touch(checkUnit);
                        if(checkColider.AcceptTouchWith(unit))
                            checkUnit.Touch(unit);

                        return;
                    }
                }
            }
        }
        #endregion
    }
}
